import { Award } from '../models';

export default function (router) {
  /**
   * 获取奖励和获赠记录
   */
  router.get('/api/front/award/records', async (ctx, next) => {
    if (!ctx.state.user) {
      await ctx.noLogin();
      next();
      return;
    }
    const { userid } = ctx.state.user;
    const { page, limit } = ctx.parsePageAndLimit(ctx.request.query);
    const total = await Award.countDocuments({ userid });
    const awards = await Award.find({ userid }, 'amount des create_time')
      .sort({ create_time: -1 })
      .skip((page - 1) * limit)
      .limit(limit);
    await ctx.json(true, 'success', { total, list: awards });
  });
}
